package com.food.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.food.dao.in.MbproductDAO;
import com.food.model.Mbproduct;



public class MbproductDAOJdbc implements MbproductDAO {
//	private static final String URL = "jdbc:sqlserver://192.168.34.108:1433;database=PJDB";
//	private static final String USER = "sa";
//	private static final String PASSWORD = "sa123456";
	
	private DataSource ds = null;
	public MbproductDAOJdbc() {
		try {
			Context ctx = new InitialContext();
			this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/food");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
    
	private static final String INSERT ="insert into mbproduct (mbid, pid) values(?,?)";
	@Override
	public void insertMbProduct(Mbproduct bean) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(INSERT);

			stmt.setInt(1, bean.getMbid());
			stmt.setInt(2, bean.getPid());
			stmt.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
    
	private static final String DELECT=
			"delete from mbproduct where mbpid =?";
	@Override
	public void deleteMbProduct (int mbpid) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(DELECT);
			stmt.setInt(1, mbpid);
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		finally{
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	  }
	}
	
	
	private static final String SELECT2 = "select mbpid, mbid, pid from mbproduct";
	@Override
	public List<Mbproduct> findAllMbProduct() {
		List<Mbproduct> result = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rset = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT2);
			rset = stmt.executeQuery();
			result = new ArrayList<Mbproduct>();
			while (rset.next()) {
				Mbproduct temp = new Mbproduct();

				temp.setMbpid(rset.getInt("mbpid"));
				temp.setMbid(rset.getInt("mbid"));
				temp.setPid(rset.getInt("pid"));


				result.add(temp);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
    
	private static final String SELECT1 ="select mbpid, mbid, pid from mbproduct where mbid=?";
	@Override
	public List<Mbproduct> findProductByMb(int mbid) {
		List<Mbproduct> result = null;
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rset = null;
		
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT1);
			stmt.setInt(1, mbid);
			rset =stmt.executeQuery();
			result = new ArrayList<Mbproduct>();
			
			while (rset.next()) {
				Mbproduct temp = new Mbproduct();

				temp.setMbpid(rset.getInt("mbpid"));
				temp.setMbid(rset.getInt("mbid"));
				temp.setPid(rset.getInt("pid"));
				
				result.add(temp);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
		
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		}
		return result;
	
	}
    
	private static final String SELECT = "select mbpid, mbid, pid from mbproduct where pid=?";
	@Override
	public List<Mbproduct> findMbByProduct(int pid) {
		List<Mbproduct> result = null;
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rset = null;
		
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT);
			stmt.setInt(1, pid);
			rset =stmt.executeQuery();
			result = new ArrayList<Mbproduct>();
			
			while (rset.next()) {
				Mbproduct temp = new Mbproduct();

				temp.setMbpid(rset.getInt("mbpid"));
				temp.setMbid(rset.getInt("mbid"));
				temp.setPid(rset.getInt("pid"));
				
				result.add(temp);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
		
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		}
		return result;
	}

	public static void main(String[] args) {
//		MbproductDAO dao = new MbproductDAOJdbc();
		
		// �d�ߩҦ�
		//List<Mbproduct> bean = dao.findAllMbProduct();
		//System.out.println(bean);
		
		//�d��mbid
		//List<Mbproduct> bean1 = dao.findProductByMb(1);
		//System.out.println(bean1);
		
		//�d��pid
		//List<Mbproduct> bean2 = dao.findMbByProduct(1);
		//System.out.println(bean2);

		
		//�s�W
		//Mbproduct bean3 = new Mbproduct();
		//bean3.setPid(10);
		//bean3.setMbid(10);
		//dao.insertMbProduct(bean3);
		//System.out.println(bean3);


	}
}
